import { createStorage } from './Storage'
const THENME_KEY = 'theme'

const storage = createStorage()

export default class Theme {
  // 初始化主题
  init() {
    const localTheme = this.get()
    if (localTheme) {
      this.setThemeAttr(localTheme)
    } else {
      this.setThemeAttr('light')
      this.set('light')
    }
  }
  get() {
    return storage.get(THENME_KEY)
  }
  set(theme: string) {
    storage.set(THENME_KEY, theme, null)
  }
  getThemeAttr() {
    return document.documentElement.getAttribute('data-theme')
  }
  setThemeAttr(theme: string) {
    document.documentElement.setAttribute('data-theme', theme)
  }
  changeTheme(theme: string) {
    this.setThemeAttr(theme)
    this.set(theme)
  }
}

export const createTheme = () => {
  return new Theme()
}
